<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>mysql学习 | 牛牛&amp;blog</title><meta name="author" content="牛牛"><meta name="copyright" content="牛牛"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="mysql学习数值类型：  字符串类型：  日期类型：  DDL(数据定义语言)data definition language-- DDL(数据定义语言)  -- 查询所有数据库SHOW DATABASES;-- 使用指定的数据库USE sqlpractice;-- 查询当前所使用的数据库SELECT DATABASE();-- 创建数据库CREATE DATABASE if not exist">
<meta property="og:type" content="article">
<meta property="og:title" content="mysql学习">
<meta property="og:url" content="https://code-ran.gitee.io/2023/03/26/mysql%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="牛牛&amp;blog">
<meta property="og:description" content="mysql学习数值类型：  字符串类型：  日期类型：  DDL(数据定义语言)data definition language-- DDL(数据定义语言)  -- 查询所有数据库SHOW DATABASES;-- 使用指定的数据库USE sqlpractice;-- 查询当前所使用的数据库SELECT DATABASE();-- 创建数据库CREATE DATABASE if not exist">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2023-03-26T15:17:04.000Z">
<meta property="article:modified_time" content="2023-11-02T03:32:07.193Z">
<meta property="article:author" content="牛牛">
<meta property="article:tag" content="mysql学习">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/img/myfavicon.png"><link rel="canonical" href="https://code-ran.gitee.io/2023/03/26/mysql%E5%AD%A6%E4%B9%A0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'mysql学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-11-02 11:32:07'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/style.css"><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (false) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/ys.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">52</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">26</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">牛牛&amp;blog</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">mysql学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-03-26T15:17:04.000Z" title="发表于 2023-03-26 23:17:04">2023-03-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-11-02T03:32:07.193Z" title="更新于 2023-11-02 11:32:07">2023-11-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/mysql%E5%AD%A6%E4%B9%A0%E7%9B%B8%E5%85%B3/">mysql学习相关</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="mysql学习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h4 id="mysql学习"><a href="#mysql学习" class="headerlink" title="mysql学习"></a>mysql学习</h4><p>数值类型：</p>
<p><img src="/2023/03/26/mysql%E5%AD%A6%E4%B9%A0/image-20230327031922634.png" alt="image-20230327031922634"></p>
<p>字符串类型：</p>
<p><img src="/2023/03/26/mysql%E5%AD%A6%E4%B9%A0/image-20230327031940127.png" alt="image-20230327031940127"></p>
<p>日期类型：</p>
<p><img src="/2023/03/26/mysql%E5%AD%A6%E4%B9%A0/image-20230327031955197.png" alt="image-20230327031955197"></p>
<h5 id="DDL-数据定义语言-data-definition-language"><a href="#DDL-数据定义语言-data-definition-language" class="headerlink" title="DDL(数据定义语言)data definition language"></a>DDL(数据定义语言)data definition language</h5><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="comment">-- DDL(数据定义语言) </span></span><br><span class="line"> </span><br><span class="line"><span class="comment">-- 查询所有数据库</span></span><br><span class="line"><span class="keyword">SHOW</span> DATABASES;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 使用指定的数据库</span></span><br><span class="line">USE sqlpractice;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询当前所使用的数据库</span></span><br><span class="line"><span class="keyword">SELECT</span> DATABASE();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建数据库</span></span><br><span class="line"><span class="keyword">CREATE</span> DATABASE if <span class="keyword">not</span> <span class="keyword">exists</span> test;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除数据库</span></span><br><span class="line"><span class="keyword">DROP</span> DATABASE test;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 注意:utf-8字符集长度占三个字节，推荐使用utf8mb4字符集，占4个字节</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询当前数据库所有表</span></span><br><span class="line"><span class="keyword">show</span> tables</span><br><span class="line"></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> employee(</span><br><span class="line">    id <span class="type">INT</span>(<span class="number">10</span>) COMMENT <span class="string">&#x27;序号&#x27;</span>,</span><br><span class="line">	name <span class="type">VARCHAR</span>(<span class="number">256</span>) COMMENT <span class="string">&#x27;名字&#x27;</span>,</span><br><span class="line">	sex TINYINT UNSIGNED COMMENT <span class="string">&#x27;性别&#x27;</span>,</span><br><span class="line">	age <span class="type">INT</span> COMMENT <span class="string">&#x27;年龄&#x27;</span></span><br><span class="line">) COMMENT <span class="string">&#x27;员工信息表&#x27;</span></span><br><span class="line"><span class="comment">-- 查询表的结构</span></span><br><span class="line"><span class="keyword">desc</span> employee</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询指定表的建表语句</span></span><br><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">table</span> employee</span><br><span class="line"></span><br><span class="line"><span class="comment">--添加字段</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> employee <span class="keyword">ADD</span> testname <span class="type">VARCHAR</span>(<span class="number">256</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">--修改字段类型和长度</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> employee MODIFY testname <span class="type">int</span>(<span class="number">10</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">--修改字段名和字段类型长度</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> employee CHANGE testname practicekey <span class="type">varchar</span>(<span class="number">256</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">--删除表中某个字段</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> employee <span class="keyword">drop</span> practicekey</span><br><span class="line"></span><br><span class="line"><span class="comment">--修改表名</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> employee RENAME <span class="keyword">TO</span> emp</span><br><span class="line"></span><br><span class="line"><span class="comment">--删除表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> emp</span><br><span class="line"></span><br><span class="line"><span class="comment">--删除表，并重新创建该表</span></span><br><span class="line"><span class="keyword">TRUNCATE</span> <span class="keyword">TABLE</span> emp</span><br><span class="line"></span><br><span class="line"><span class="keyword">desc</span> emp</span><br></pre></td></tr></table></figure>

<h4 id="DML（数据操作语言）data-manipulation-language"><a href="#DML（数据操作语言）data-manipulation-language" class="headerlink" title="DML（数据操作语言）data manipulation language"></a>DML（数据操作语言）data manipulation language</h4><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="comment">-- 添加数据</span></span><br><span class="line"><span class="comment">-- 指定字段</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp ( id, NAME, sex, age )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">	(<span class="number">1</span>,<span class="string">&#x27;张三&#x27;</span>,<span class="string">&#x27;男&#x27;</span>,<span class="number">25</span>),(<span class="number">2</span>,<span class="string">&#x27;李四&#x27;</span>,<span class="string">&#x27;男&#x27;</span>,<span class="number">26</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 全部字段</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp <span class="keyword">VALUES</span>(<span class="number">3</span>,<span class="string">&#x27;王五&#x27;</span>,<span class="string">&#x27;男&#x27;</span>,<span class="number">27</span>)</span><br><span class="line"></span><br><span class="line">注意:字符串和日期类型的数据应该包含在引号中。而且插入的数据大小应该在字段规定的范围内。</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 更新和删除数据</span></span><br><span class="line">UPDATE emp <span class="keyword">SET</span> id <span class="operator">=</span> <span class="number">3</span>,name <span class="operator">=</span> <span class="string">&#x27;小美&#x27;</span>,sex <span class="operator">=</span> <span class="string">&#x27;女&#x27;</span>, age <span class="operator">=</span> <span class="number">18</span> <span class="keyword">where</span> id <span class="operator">=</span> <span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">DELETE</span> <span class="keyword">from</span> emp <span class="keyword">WHERE</span> id <span class="operator">=</span> <span class="number">3</span></span><br></pre></td></tr></table></figure>

<h4 id="DQL（数据查询语言）data-query-language"><a href="#DQL（数据查询语言）data-query-language" class="headerlink" title="DQL（数据查询语言）data query language"></a>DQL（数据查询语言）data query language</h4><h5 id="1-创建表："><a href="#1-创建表：" class="headerlink" title="(1)创建表："></a>(1)创建表：</h5><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> emp (</span><br><span class="line">  id <span class="type">int</span> COMMENT <span class="string">&#x27;编号&#x27;</span>,</span><br><span class="line">	workno <span class="type">VARCHAR</span>(<span class="number">10</span>) COMMENT <span class="string">&#x27;工号&#x27;</span>,</span><br><span class="line">	name <span class="type">VARCHAR</span>(<span class="number">10</span>) COMMENT <span class="string">&#x27;姓名&#x27;</span>,</span><br><span class="line">	gender <span class="type">char</span>(<span class="number">1</span>) COMMENT <span class="string">&#x27;性别&#x27;</span>,</span><br><span class="line">	age TINYINT UNSIGNED COMMENT <span class="string">&#x27;年龄&#x27;</span>,</span><br><span class="line">	idcard <span class="type">CHAR</span>(<span class="number">18</span>) COMMENT <span class="string">&#x27;身份证号&#x27;</span>,</span><br><span class="line">	workaddress <span class="type">VARCHAR</span>(<span class="number">50</span>) COMMENT <span class="string">&#x27;工作地址&#x27;</span>,</span><br><span class="line">	entrydate <span class="type">date</span> COMMENT <span class="string">&#x27;入职时间&#x27;</span></span><br><span class="line">) COMMENT <span class="string">&#x27;员工信息表&#x27;</span></span><br></pre></td></tr></table></figure>

<h5 id="2-构造测试数据："><a href="#2-构造测试数据：" class="headerlink" title="(2)构造测试数据："></a>(2)构造测试数据：</h5><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp (id, workno, name, gender, age, idcard, workaddress, entrydate)</span><br><span class="line"><span class="keyword">VALUES</span> </span><br><span class="line">    (<span class="number">2</span>, <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;李四&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="number">28</span>, <span class="string">&#x27;230103198603010087&#x27;</span>, <span class="string">&#x27;北京市朝阳区YY街22号&#x27;</span>, <span class="string">&#x27;2020-12-15&#x27;</span>),</span><br><span class="line">    (<span class="number">3</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;王五&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="number">32</span>, <span class="string">&#x27;310115198912312345&#x27;</span>, <span class="string">&#x27;北京市朝阳区YY街22号&#x27;</span>, <span class="string">&#x27;2022-01-10&#x27;</span>),</span><br><span class="line">    (<span class="number">4</span>, <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;赵六&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="number">24</span>, <span class="string">&#x27;440118199807170090&#x27;</span>, <span class="string">&#x27;深圳市南山区PP街66号&#x27;</span>, <span class="string">&#x27;2021-05-08&#x27;</span>),</span><br><span class="line">    (<span class="number">5</span>, <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;钱七&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="number">29</span>, <span class="string">&#x27;330203199512121234&#x27;</span>, <span class="string">&#x27;杭州市西湖区QQ路123号&#x27;</span>, <span class="string">&#x27;2022-02-21&#x27;</span>),</span><br><span class="line">    (<span class="number">6</span>, <span class="string">&#x27;6&#x27;</span>, <span class="string">&#x27;孙八&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="number">26</span>, <span class="string">&#x27;320205199303051234&#x27;</span>, <span class="string">&#x27;南京市鼓楼区WW街9号&#x27;</span>, <span class="string">&#x27;2021-09-01&#x27;</span>),</span><br><span class="line">    (<span class="number">7</span>, <span class="string">&#x27;7&#x27;</span>, <span class="string">&#x27;周九&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="number">31</span>, <span class="string">&#x27;510108198512122345&#x27;</span>, <span class="string">&#x27;成都市青羊区DD路3号&#x27;</span>, <span class="string">&#x27;2022-03-15&#x27;</span>),</span><br><span class="line">    (<span class="number">8</span>, <span class="string">&#x27;8&#x27;</span>, <span class="string">&#x27;吴十&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="number">27</span>, <span class="string">&#x27;350303199407010001&#x27;</span>, <span class="string">&#x27;福州市鼓楼区EE街21号&#x27;</span>, <span class="string">&#x27;2021-11-11&#x27;</span>),</span><br><span class="line">    (<span class="number">9</span>, <span class="string">&#x27;9&#x27;</span>, <span class="string">&#x27;郑十一&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="number">30</span>, <span class="string">&#x27;440305198703060009&#x27;</span>, <span class="string">&#x27;深圳市宝安区男男路22号&#x27;</span>, <span class="string">&#x27;2022-01-02&#x27;</span>),</span><br><span class="line">    (<span class="number">10</span>, <span class="string">&#x27;10&#x27;</span>, <span class="string">&#x27;王十二&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="number">24</span>, <span class="string">&#x27;210114199810082345&#x27;</span>, <span class="string">&#x27;沈阳市和平区GG路6号&#x27;</span>, <span class="string">&#x27;2021-06-30&#x27;</span>),</span><br><span class="line">    (<span class="number">11</span>, <span class="string">&#x27;11&#x27;</span>, <span class="string">&#x27;李十三&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="number">33</span>, <span class="string">&#x27;110108198511112245&#x27;</span>, <span class="string">&#x27;北京市海淀区HH街88号&#x27;</span>, <span class="string">&#x27;2022-03-20&#x27;</span>),</span><br><span class="line">    (<span class="number">12</span>, <span class="string">&#x27;12&#x27;</span>, <span class="string">&#x27;张十四&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="number">28</span>, <span class="string">&#x27;420102199302030001&#x27;</span>, <span class="string">&#x27;上海市浦东区II路55号&#x27;</span>, <span class="string">&#x27;2021-12-25&#x27;</span>),</span><br><span class="line">    (<span class="number">13</span>, <span class="string">&#x27;13&#x27;</span>, <span class="string">&#x27;赵十五&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="number">26</span>, <span class="string">&#x27;320205199511111234&#x27;</span>, <span class="string">&#x27;南京市建邺区JJ街66号&#x27;</span>, <span class="string">&#x27;2022-02-14&#x27;</span>),</span><br><span class="line">    (<span class="number">14</span>, <span class="string">&#x27;14&#x27;</span>, <span class="string">&#x27;孙十六&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="number">29</span>, <span class="string">&#x27;330203199212120001&#x27;</span>, <span class="string">&#x27;杭州市江干区KK路77号&#x27;</span>, <span class="string">&#x27;2021-09-18&#x27;</span>),</span><br><span class="line">    (<span class="number">15</span>, <span class="string">&#x27;15&#x27;</span>, <span class="string">&#x27;周十七&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="number">27</span>, <span class="string">&#x27;510108199407152345&#x27;</span>, <span class="string">&#x27;成都市锦江区LL街4号&#x27;</span>, <span class="string">&#x27;2022-04-01&#x27;</span>),</span><br><span class="line">    (<span class="number">16</span>, <span class="string">&#x27;16&#x27;</span>, <span class="string">&#x27;吴十八&#x27;</span>, <span class="string">&#x27;男&#x27;</span>, <span class="number">25</span>, <span class="string">&#x27;350303199612200009&#x27;</span>, <span class="string">&#x27;福州市台江区女女路99号&#x27;</span>, <span class="string">&#x27;2021-11-30&#x27;</span>),</span><br><span class="line">    (<span class="number">17</span>, <span class="string">&#x27;17&#x27;</span>, <span class="string">&#x27;郑十九&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="number">32</span>, <span class="string">&#x27;440305198901230001&#x27;</span>, <span class="string">&#x27;深圳市龙岗区NN路33号&#x27;</span>, <span class="string">&#x27;2022-03-05&#x27;</span>),</span><br><span class="line">    (<span class="number">18</span>, <span class="string">&#x27;18&#x27;</span>, <span class="string">&#x27;李二十&#x27;</span>, <span class="string">&#x27;女&#x27;</span>, <span class="number">31</span>, <span class="keyword">NULL</span>, <span class="string">&#x27;深圳市福田区OO路11号&#x27;</span>, <span class="string">&#x27;2021-08-02&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h5 id="3-使用DQL进行基础查询"><a href="#3-使用DQL进行基础查询" class="headerlink" title="(3)使用DQL进行基础查询"></a>(3)使用DQL进行基础查询</h5><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="comment">-- 查询id,name,work,age 返回</span></span><br><span class="line"><span class="keyword">SELECT</span> id, name,workno,age <span class="keyword">FROM</span> emp</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有字段返回</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp</span><br><span class="line">注意:</span><br><span class="line">虽然使用 <span class="keyword">SELECT</span> <span class="operator">*</span> 可以返回表中的所有列，但是它可能会导致一些问题，比如：</span><br><span class="line">性能问题：<span class="keyword">SELECT</span> <span class="operator">*</span> 会返回表中的所有列，即使应用程序只需要其中的一部分数据。这增加了数据传输和处理的负担，并且可能会导致查询速度变慢。</span><br><span class="line">冗余数据：如果表结构发生变化，或者新的列被添加到表中，<span class="keyword">SELECT</span> <span class="operator">*</span> 的结果也会随之改变。这可能会破坏应用程序的逻辑和功能。</span><br><span class="line">安全问题：使用 <span class="keyword">SELECT</span> <span class="operator">*</span> 可能会泄露敏感信息。比如一个包含密码的列可能被无意间<span class="keyword">SELECT</span>出来并显示在用户界面上。</span><br><span class="line">因此，为了提高查询性能、减少冗余数据、保护敏感信息，我们应该尽量避免使用 <span class="keyword">SELECT</span> <span class="operator">*</span>，而是明确指定需要查询的列。</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有员工的工作地址，并起别名</span></span><br><span class="line"><span class="keyword">SELECT</span> workaddress <span class="keyword">as</span> address <span class="keyword">from</span> emp</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询公司员工的上班地址，并且不能重复</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> workaddress  <span class="keyword">from</span> emp</span><br></pre></td></tr></table></figure>

<h5 id="4-使用DQL进行条件查询"><a href="#4-使用DQL进行条件查询" class="headerlink" title="(4)使用DQL进行条件查询"></a>(4)使用DQL进行条件查询</h5><p>语法：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表名 <span class="keyword">WHERE</span> 条件列表</span><br></pre></td></tr></table></figure>

<p><img src="/2023/03/26/mysql%E5%AD%A6%E4%B9%A0/gitee\code-ran\source_posts\Potplayer相关设置优化\image-20230327013958492.png" alt="image-20230327013958492"></p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="comment">-- 查询没有身份证号的员工信息</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> idcard <span class="keyword">IS</span> <span class="keyword">NULL</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询有身份证号的员工信息</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> idcard <span class="keyword">IS</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄为26,27,28的员工的信息</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> age <span class="operator">=</span> <span class="number">26</span> <span class="keyword">OR</span> age <span class="operator">=</span> <span class="number">27</span> <span class="keyword">OR</span> age <span class="operator">=</span> <span class="number">28</span></span><br><span class="line">或者</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> age <span class="keyword">in</span> (<span class="number">26</span>,<span class="number">27</span>,<span class="number">28</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询姓名为两个字的员工信息</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> name <span class="keyword">LIKE</span> <span class="string">&#x27;__&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询身份证号最后一位是x的员工信息</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> idcard <span class="keyword">LIKE</span> <span class="string">&#x27;%x&#x27;</span></span><br></pre></td></tr></table></figure>

<h5 id="5-聚合函数"><a href="#5-聚合函数" class="headerlink" title="(5)聚合函数"></a>(5)聚合函数</h5><p> 将一列数据作为一个整体，进行纵向计算。</p>
<p>常见聚合函数：</p>
<table>
<thead>
<tr>
<th>函数</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>count</td>
<td>统计数量</td>
</tr>
<tr>
<td>max</td>
<td>最大值</td>
</tr>
<tr>
<td>min</td>
<td>最小值</td>
</tr>
<tr>
<td>avg</td>
<td>平均值</td>
</tr>
<tr>
<td>sum</td>
<td>求和</td>
</tr>
</tbody></table>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="comment">-- 统计该企业员工数量</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">FROM</span> emp</span><br><span class="line"></span><br><span class="line">注意：<span class="keyword">null</span>值不参与所有聚合函数的运算</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的平均年龄</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(age) <span class="keyword">FROM</span> emp</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的最大年龄</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MAX</span>(age) <span class="keyword">FROM</span> emp</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的最小年龄</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MIN</span>(age) <span class="keyword">FROM</span> emp</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计杭州地区的员工年龄之和</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">SUM</span>(age) <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> workaddress <span class="keyword">LIKE</span> <span class="string">&#x27;杭州市%&#x27;</span></span><br></pre></td></tr></table></figure>



<p>(5)使用DQL进行分组查询</p>
<p>语法：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表名 [<span class="keyword">WHERE</span> 条件] <span class="keyword">GROUP</span> <span class="keyword">BY</span> 分组字段名 [<span class="keyword">HAVING</span> 分组后过滤条件]</span><br></pre></td></tr></table></figure>

<h5 id="6-排序查询"><a href="#6-排序查询" class="headerlink" title="(6)排序查询"></a>(6)排序查询</h5><p>语法：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表名 <span class="keyword">ORDER</span> <span class="keyword">BY</span> 字段<span class="number">1</span>，排序方式<span class="number">1</span>，字段<span class="number">2</span> 排序方式<span class="number">2</span>；</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">注意：如果是多字段排序，当第一个字段值相同时，才会根据第二个字段进行排序。</span><br><span class="line"><span class="keyword">asc</span><span class="comment">--升序（默认）</span></span><br><span class="line"><span class="keyword">desc</span> <span class="comment">--降序</span></span><br><span class="line"><span class="comment">-- 根据年龄对公司的员工进行升序排序</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">ORDER</span> <span class="keyword">BY</span> age </span><br><span class="line"></span><br><span class="line"><span class="comment">-- 根据入职时间，对员工进行降序排序</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">ORDER</span> <span class="keyword">BY</span> entrydate <span class="keyword">DESC</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 根据年龄对公司的员工进行升序排序，年龄相同，再按照入职时间进行降序排序</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">ORDER</span> <span class="keyword">BY</span> age <span class="keyword">ASC</span>,entrydate <span class="keyword">DESC</span></span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://code-ran.gitee.io">牛牛</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://code-ran.gitee.io/2023/03/26/mysql%E5%AD%A6%E4%B9%A0/">https://code-ran.gitee.io/2023/03/26/mysql%E5%AD%A6%E4%B9%A0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://code-ran.gitee.io" target="_blank">牛牛&blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/mysql%E5%AD%A6%E4%B9%A0/">mysql学习</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/06/01/nacos/"><img class="prev-cover" src="" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">nacos</div></div></a></div><div class="next-post pull-right"><a href="/2023/03/01/java%E4%BD%BF%E7%94%A8file-delete%E6%96%B9%E6%B3%95%E5%88%A0%E9%99%A4%E6%96%87%E4%BB%B6%E5%A4%B1%E8%B4%A5/"><img class="next-cover" src="" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">java使用file.delete方法删除文件失败</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/ys.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">牛牛</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">52</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">26</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/code-ran"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">本blog内容仅用于个人学习使用!</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#mysql%E5%AD%A6%E4%B9%A0"><span class="toc-number">1.</span> <span class="toc-text">mysql学习</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#DDL-%E6%95%B0%E6%8D%AE%E5%AE%9A%E4%B9%89%E8%AF%AD%E8%A8%80-data-definition-language"><span class="toc-number">1.1.</span> <span class="toc-text">DDL(数据定义语言)data definition language</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#DML%EF%BC%88%E6%95%B0%E6%8D%AE%E6%93%8D%E4%BD%9C%E8%AF%AD%E8%A8%80%EF%BC%89data-manipulation-language"><span class="toc-number">2.</span> <span class="toc-text">DML（数据操作语言）data manipulation language</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#DQL%EF%BC%88%E6%95%B0%E6%8D%AE%E6%9F%A5%E8%AF%A2%E8%AF%AD%E8%A8%80%EF%BC%89data-query-language"><span class="toc-number">3.</span> <span class="toc-text">DQL（数据查询语言）data query language</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-%E5%88%9B%E5%BB%BA%E8%A1%A8%EF%BC%9A"><span class="toc-number">3.1.</span> <span class="toc-text">(1)创建表：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-%E6%9E%84%E9%80%A0%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%EF%BC%9A"><span class="toc-number">3.2.</span> <span class="toc-text">(2)构造测试数据：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-%E4%BD%BF%E7%94%A8DQL%E8%BF%9B%E8%A1%8C%E5%9F%BA%E7%A1%80%E6%9F%A5%E8%AF%A2"><span class="toc-number">3.3.</span> <span class="toc-text">(3)使用DQL进行基础查询</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-%E4%BD%BF%E7%94%A8DQL%E8%BF%9B%E8%A1%8C%E6%9D%A1%E4%BB%B6%E6%9F%A5%E8%AF%A2"><span class="toc-number">3.4.</span> <span class="toc-text">(4)使用DQL进行条件查询</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0"><span class="toc-number">3.5.</span> <span class="toc-text">(5)聚合函数</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#6-%E6%8E%92%E5%BA%8F%E6%9F%A5%E8%AF%A2"><span class="toc-number">3.6.</span> <span class="toc-text">(6)排序查询</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/04/30/springboot%E6%B3%A8%E8%A7%A3%E7%9B%B8%E5%85%B3/" title="springboot注解相关">springboot注解相关</a><time datetime="2024-04-30T03:20:37.000Z" title="发表于 2024-04-30 11:20:37">2024-04-30</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/04/30/CommandLineRunner%E5%92%8CApplicationRunner%E7%94%A8%E6%B3%95%E4%B8%8E%E5%8C%BA%E5%88%AB/" title="CommandLineRunner和ApplicationRunner用法与区别">CommandLineRunner和ApplicationRunner用法与区别</a><time datetime="2024-04-30T02:42:21.000Z" title="发表于 2024-04-30 10:42:21">2024-04-30</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/08/%E5%A5%BD%E6%96%87%E6%94%B6%E8%97%8F/" title="好文收藏">好文收藏</a><time datetime="2023-12-08T08:40:13.000Z" title="发表于 2023-12-08 16:40:13">2023-12-08</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/08/springboot%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD/" title="springboot文件上传下载">springboot文件上传下载</a><time datetime="2023-12-08T03:30:54.000Z" title="发表于 2023-12-08 11:30:54">2023-12-08</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/05/linux%E4%BD%BF%E7%94%A8curl%E5%91%BD%E4%BB%A4%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82/" title="linux使用curl命令发送请求">linux使用curl命令发送请求</a><time datetime="2023-12-05T05:56:19.000Z" title="发表于 2023-12-05 13:56:19">2023-12-05</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2024 By 牛牛</div><div class="footer_custom_text">Hi, Welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">簡</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>